package com.zsc.rediscli.sentinel;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 *   master故障自动转移测试
 *   1.用kill命令直接将7000的master节点宕机
 *   2.当sentinel监测到master宕机，让其中一个slave晋升为master
 */
public class SentinelTest {

    public static void main(String[] args) {
        Set<String> sentinels = new HashSet<>();
        sentinels.add("192.168.56.108:26379");
        sentinels.add("192.168.56.108:26380");
        sentinels.add("192.168.56.108:26381");
        //jedis实现对redis sentinel的客户端连接
        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("mymaster",sentinels);
        int count = 0;
        while (true){
            count ++;
            Jedis jedis = null;
            try {
                jedis = jedisSentinelPool.getResource();
                int i = new Random().nextInt(100000);
                String key = "k-" + i;
                String value = "v-" + i;
                jedis.set(key,value);
                if (count % 100 == 0)
                    System.out.println(jedis.get(key));
                TimeUnit.MILLISECONDS.sleep(10);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (jedis != null){
                    jedis.close();
                }
            }


        }
    }
}
